<%@ page import="javax.servlet.jsp.JspWriter" pageEncoding="utf-8"%>
<%@ page import="com.flute.haflute.web.TopoQuery"%>
<%@ page import="com.flute.haflute.web.NodeEntity"%>
<%@ page import="com.flute.haflute.monitor.JobMonitorInfo"%>
<%@ page import="com.flute.haflute.monitor.ClusterStatusMonitor"%>
<%@ page import="com.flute.haflute.web.JobService4Web"%>
<%@ page import="com.flute.haflute.monitor.SystemAbility"%>
<%@ page language="java" import="java.util.*" %>
<%


  String path = request.getContextPath();
  String issuccess = request.getParameter("issuccess");
  String sAction = request.getParameter("action");
    if(sAction != null) {
        if(sAction.equalsIgnoreCase("logout")) {
            // Logging out.
            session = request.getSession();
            if (session != null) {
                session.invalidate();
                // Redirect back to here and we'll get thrown to the login
                // page.
                response.sendRedirect(path + "/index2.jsp"); 
            }
        }
    }

    String title = "管理员控制台";
    int tab = 0;
    String flag = request.getParameter("flag");
    String stopip = request.getParameter("stopip");
    if(flag!=null){
    	JobService4Web.endJobByJobMaster(stopip); 

    }
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><%=title %></title>
<link href="<%=path%>/css/admin.css" rel="stylesheet" type="text/css"/>
<link href="<%=path%>/css/pub.css" rel="stylesheet" type="text/css"/>
<script language="javascript" type="text/javascript" src="<%=path%>/js/sort.js"></script>
<script type="text/javascript">
			function delete_confirm(jobDesc){ 
				var k = window.confirm("可能导致作业状态不可恢复,你确定要关闭该作业?\n" + jobDesc, false); 
				if(k){ 
					return true; 
				}else{    
					return false; 
				}
			}
		</script>
</head>
<body>
<!--heaer start-->
<div class="header"><img src="../images/SystemName.gif" /></div>
<!--heaer end-->
<!--content start-->
<div class="content">
    <jsp:include page="page/left_menu_admin.jsp"></jsp:include>
	<div class="right">
	    <div class="label">
		    <ul>
			     <li><a href='/allhistoryjobs.jsp'>所有历史任务</a></li>
				 <li><a href='/historymonitorparamlist.jsp'>历史监控信息列表</a></li>
				 <li><a href='/monitorparamlist.jsp'>监控列表</a></li>
				 <li><a href='/nodechecked.jsp'>作业调度策略</a></li>
				 <div class="clear"></div>
			</ul>
		</div>
		
		<div class="title">节点状态:</div>
		<table width="940" border="0" cellspacing="1" class="sortable" id="mytable" >
		  <tr class="h_tr">
			<td width="98" height="18" bgcolor="#f3f5f8">角色</td>
			<td width="150" align='left' bgcolor="#f3f5f8">IP地址</td>
			<td width="55">状态</td>
			<td width="65">作业情况</td>
			<td width="65" bgcolor="#f3f5f8">CPU数</td>
			<td width="120" bgcolor="#f3f5f8">物理内存总量(MB)</td>
			<td width="145" bgcolor="#f3f5f8">空闲物理内存总量(MB)</td>
			<td width="150" bgcolor="#f3f5f8">JVM内存占用率</td>
		  </tr>
		 <tr>
    	<td align='left'>指定主节点</td>
    	<% NodeEntity ne = TopoQuery.getDM();
    	    SystemAbility systemability = ClusterStatusMonitor.getSystemAbility(ne.getIp());
    		out.println("<td align='left'><a href='/jobmonitor.jsp?ip="+ ne.getIp()+"'>" + ne.getIp() + "</a></td><td align='left'>" + ne.getStatus() +"</td><td align='left'>"+ne.getNodeIdleState()+"</td>");
    		if(systemability!=null){
    			out.println("<td align='left'>"+systemability.getTotalCpuCores()+"</td>");
        		out.println("<td align='left'>"+systemability.getTotalPhysicalMemorySize() +"</td>");
        		out.println("<td align='left'>"+systemability.getFreePhysicalMemorySize() +"</td>");
        		out.println("<td align='left'>"+systemability.getJVMMemoryRate()+"</td>");
    		}else{
    			out.println("<td>-</td>");
    			out.println("<td>-</td>");
    			out.println("<td>-</td>");
    			out.println("<td>-</td>");
    		}
    		
    		
    	%>
    </tr>
	    <tr>
    	<td align='left'>备份主节点</td>
    	<% ne = TopoQuery.getBDM();
    	   SystemAbility systemability_bak = ClusterStatusMonitor.getSystemAbility(ne.getIp());
    	  
       		out.println("<td align='left'><a href='/jobmonitor.jsp?ip="+ ne.getIp()+"'>" + ne.getIp() + "</a></td><td align='left'>" + ne.getStatus() +"</td><td align='left'>"+ne.getNodeIdleState()+"</td>");
    		
    		if(systemability_bak!=null){
    			out.println("<td align='left'>"+systemability_bak.getTotalCpuCores()+"</td>");
        		out.println("<td align='left'>"+systemability_bak.getTotalPhysicalMemorySize()+"</td>");
        		out.println("<td align='left'>"+systemability_bak.getFreePhysicalMemorySize()+"</td>");
        		out.println("<td align='left'>"+systemability_bak.getJVMMemoryRate()+"</td>");
    		}else{
    			out.println("<td>-</td>");
    			out.println("<td>-</td>");
    			out.println("<td>-</td>");
    			out.println("<td>-</td>");
    		}
    		
    	%>
    </tr>
    <tr>
     <% 
    	NodeEntity[] allNodes = TopoQuery.getOrdinaryNodes();
    	for(int i=0; i<allNodes.length; i++) {
    		 SystemAbility systemability_other = ClusterStatusMonitor.getSystemAbility(allNodes[i].getIp());
    		out.println("<tr>\n<td align='left'>" + allNodes[i].getRole() + "</td>\n<td align='left'><a href='/jobmonitor.jsp?ip="+allNodes[i].getIp()+"' >" + allNodes[i].getIp() + "</td>\n<td>" + allNodes[i].getStatus() + "</td><td>"+ allNodes[i].getNodeIdleState() +"</td>");
    		if(systemability_other!=null){
    			out.println("<td align='left'>"+systemability_other.getTotalCpuCores()+"</td>");
        		out.println("<td align='left'>"+systemability_other.getTotalPhysicalMemorySize()+"</td>");
        		out.println("<td align='left'>"+systemability_other.getFreePhysicalMemorySize()+"</td>");
        		out.println("<td align='left'>"+systemability_other.getJVMMemoryRate()+"</td>");
    		}else{
    			out.println("<td>-</td>");
    			out.println("<td>-</td>");
    			out.println("<td>-</td>");
    			out.println("<td>-</td>");
    		}
    		
    	}    
    %>
    </tr> 
	  </table>
	  <div class="title">作业状态：</div>
      <table width="940" border="0" cellspacing="1"  >
		  <tr  class="h_tr">
			<td width="120"  height="18" bgcolor="#f3f5f8">作业ID</td>
			<td width="120"  bgcolor="#f3f5f8">IP地址</td>
			<td width="120"  bgcolor="#f3f5f8">子节点</td>
			<td width="100"  bgcolor="#f3f5f8">作业状态</td>
			<td width="140"  bgcolor="#f3f5f8">作业描述</td>
			<td width="140"  bgcolor="#f3f5f8">作业创建时间</td>
			<td width="120" >操作</td>
			
		  </tr>
	 <%
        	List<JobMonitorInfo> jobinfos = ClusterStatusMonitor.getJobInfo();
        	if (jobinfos != null&&jobinfos.size()>0) {
        		for (int i = 0; i < jobinfos.size(); i++) {
        			JobMonitorInfo jminfo = (JobMonitorInfo) jobinfos.get(i);
        			List<String> childs = jminfo.getChilds();
        			
        			String childstr = "";
        			if (childs != null && childs.size() > 0) {
        				for (int j = 0; j < childs.size(); j++) {
        					childstr = childstr + childs.get(j) + "<br>";
        				}
        				if (!childstr.equals("")) {
        					childstr.substring(0, childstr.length() - 1);
        				}
        			} else {
        				childstr = "-";
        			}
        
        %>
        
	    <tr>
	        <td><a href='./monitorparamlist.jsp?ip=<%=jminfo.getMaster().toString()%>'><%=jminfo.getJobid()%></a></td>
	    	<td><%=jminfo.getMaster().toString()%></td>
	    	<td><%=childstr%></td>
	    	<td><%=jminfo.getJobstatus().toString()%></td>
	    	<td><%=jminfo.getJobDescription()==null?"-":jminfo.getJobDescription()%></td>
	    	<td><%=jminfo.getReceiveDate()==null?"-":jminfo.getReceiveDate()%></td>
	    	<td><a href='./index.jsp?flag=1&stopip=<%=jminfo.getMaster().toString()%>' onclick='return delete_confirm("作业ID: " + "<%=jminfo.getJobid()%>" + "\n描述: " + "<%=jminfo.getJobDescription()%>")'>终止</a></td>
	    </tr>
    
    <%
        	 }
        	}
        %>
	  </table>
	  <div class="title"><input class="button" type="button" value="刷新" onClick="window.location='<%=request.getContextPath()%>'" /></div>
	</div>
	<div class="clear"></div>
</div>
<!--content end-->
<div class="footer"></div>
</body>
</html>